import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { getScreenSize, goBack} from "../../utils/utils";

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		// 输入框数值
		inputValue: '',
		// 类型列表
		garbageList: [
			{
				id: '1',
				text: '可回收物',
				desc: '能卖钱的、可回收利用的',
				url: 'https://image-classify.daieco.com/kejian-quanzi/T8HBa4hhaeTMf3dcebe662f1975d4503c968a0c17a1e.png',
			},
			{
				id: '2',
				text: '厨余垃圾',
				desc: '易腐烂、易生细菌',
				url: 'https://image-classify.daieco.com/kejian-quanzi/zp2acju25dK61ae82072af81072793ca2e7d7e21f6b9.png',
			},
			{
				id: '3',
				text: '其他垃圾',
				desc: '难以辨认的废弃垃圾',
				url: 'https://image-classify.daieco.com/kejian-quanzi/cSQj563AGLTWfdafdc0376099e40fdc3292dcc611069.png',
			},
			{
				id: '4',
				text: '有害垃圾',
				desc: '容易污染环境的废弃垃圾',
				url: 'https://image-classify.daieco.com/kejian-quanzi/7NYMLhL0UG7Gc5cdf04d18d26ec2cc0bf236c7f40a16.png',
			},
		],
		// 页面传递过来的参数
		params: {},
		// 物品类型
		goodsType: null,
		contentHeight: 390,
	},
	// 加载loading
	loading: false,

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log('纠错页面参数', options);
		let { screenHeight } = getScreenSize()
		this.setData({
			params: options,
			contentHeight: screenHeight/2 + 50
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		this.modal = this.selectComponent('#modal')
	},
	// 打开确认弹窗
	showModal(e) {
		this.setData({
			goodsType: e.currentTarget.dataset.text
		}, () => {
			this.modal.show()
		})
	},
	// 反馈提交
	async feedback(e) {
		if (this.loading) return
		this.loading = true
		const { goodsType, inputValue, params } = this.data
		const { url, goodsName } = params || {}

		const res = await request.post(api.userFeedback, {
			imageUrl: url,
			remark: e?.currentTarget.dataset.text || '',
			garbageType: ['', '可回收物', '厨余垃圾', '其他垃圾', '有害垃圾'].indexOf(goodsType),
			itemName: inputValue || goodsName,
		})

		if (res?.httpStatus === 200) {
			wx.showToast({
				title: '提交成功!',
				icon: 'success',
				duration: 1500,
			})
			this.modal.hide()
			this.setData({
				inputValue: ''
			})
			setTimeout(() => {
				goBack()
			}, 1000)
		}
		this.loading = false
	},
	// 获取input输入值
	inputChange(e) {
		this.setData({
			inputValue: e.detail.value,
		})
	},
	// 用户点击右上角分享
	onShareAppMessage(e) {
		return shareOptions('default',e)
	},
});
